package com.foreknow.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.ConstantItem;
import com.foreknow.entity.Department;
import com.foreknow.service.ConstantItemService;
import com.foreknow.utils.Constance;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/consItem")
public class ConstantItemController {
    @Autowired
    private ConstantItemService constantItemService;

    @RequestMapping(value="/findConstantItem",method = RequestMethod.GET)
    public List<ConstantItem> findUserByPage(@RequestParam Integer typeId) throws JsonProcessingException {
        return constantItemService.queryConstantItemByTypeId(typeId);
    }
    @RequestMapping(value = "/insertConsItem",method = RequestMethod.GET)
    public String insertConsItem(@RequestParam Map<String,Object> para) throws JsonProcessingException {
        String ConstantCode = (String)para.get("constantCode");
        String ConstantName = (String)para.get("constantName");
        Integer ConstantTypeID =Integer.parseInt((String) para.get("constantTypeID"));
        Integer DelMark =Integer.parseInt((String) para.get("DelMark"));
        ConstantItem constantItem = new ConstantItem();
        constantItem.setConstantcode(ConstantCode);
        constantItem.setConstantname(ConstantName);
        constantItem.setConstanttypeid(ConstantTypeID);
        constantItem.setDelmark(DelMark);
        HashMap<String,Object> hs=new HashMap<>();
        int i = constantItemService.insertCons(constantItem);
        hs.put("data",i);
        if (i>0){
            hs.put("meta",new Constance("添加用户成功",200));
        }else {
            hs.put("meta",new Constance("添加用户失败",403));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
    @RequestMapping(value = "/deleteConsItemById")
    public String deleteConsItemById(@RequestBody Integer id)throws JsonProcessingException {
        int i = constantItemService.deleteConsById(id);
        HashMap<String,Object> hs=new HashMap<>();
        hs.put("data",i);
        if (i>0){
            hs.put("meta",new Constance("删除常数成功",200));
        }else {
            hs.put("meta",new Constance("删除常数失败",403));
        }

        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
    @RequestMapping(value = "/deleteConsItemByIds",method = RequestMethod.GET)
    public String deleteConsItemByIds(@RequestParam Map<String,List<Integer>> ids) throws JsonProcessingException {
        Integer i = constantItemService.deleteConsByIds(ids.get("ids"));
        HashMap<String,Object> hs=new HashMap<>();
        hs.put("data",i);
        if (i>0){
            hs.put("meta",new Constance("删除常数成功",200));
        }else {
            hs.put("meta",new Constance("删除常数失败",403));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
    @RequestMapping(value = "/updateConsItemById",method = RequestMethod.GET)
    public String updateConsItemById(@RequestBody ConstantItem constantItem)throws JsonProcessingException {
        int i = constantItemService.updateById(constantItem);
        HashMap<String,Object> hs=new HashMap<>();
        hs.put("data",i);
        if (i>0){
            hs.put("meta",new Constance("修改常数成功",200));
        }else {
            hs.put("meta",new Constance("修改常数失败",403));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
    @RequestMapping(value = "/findConsItem",method = RequestMethod.GET)
    public String findConsItem(@RequestParam Map<String,Object> para) throws JsonProcessingException {
        //获取参数
        String param = (String)para.get("query");
        Integer pageNum =Integer.parseInt((String) para.get("pageNum"));
        Integer pageSize=Integer.parseInt((String) para.get("pageSize"));
        HashMap<String,Object> hs=new HashMap<>();
        //设置分页信息
        PageHelper.startPage(pageNum,pageSize);
        List<ConstantItem> itemList = constantItemService.findConsByPage(param);
        PageInfo<ConstantItem> pageInfo = new PageInfo<>(itemList);
        hs.put("totalpage",pageInfo.getTotal());
        hs.put("pagenum",pageInfo.getPageNum());
        hs.put("constantItem",itemList);
        if (itemList.size()>0){
            hs.put("meta",new Constance("获取成功",200));
        }else {
            hs.put("meta",new Constance("获取失败",403));
        }

        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
    @RequestMapping(value = "/findConsItemById",method = RequestMethod.GET)
    public String findConsItemById(@RequestParam Integer id) throws JsonProcessingException {
        ConstantItem id1 = constantItemService.findConsById(id);
        HashMap<String,Object> hs=new HashMap<>();
        hs.put("data",id1);
        if (id1!=null){
            hs.put("meta", new Constance("查询成功", 200));
        }else {
            hs.put("meta", new Constance("查询失败", 403));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
    @RequestMapping(value = "/searchConsItem",method = RequestMethod.POST)
    public String searchConsItem(@RequestBody Map<String,Object> para) throws JsonProcessingException {
        //获取参数
        ObjectMapper objectMapper = new ObjectMapper();
        Object heightQuery = para.get("heightQuery");
        ConstantItem constantItem = objectMapper.convertValue(heightQuery, ConstantItem.class);
        Integer pageNum = Integer.parseInt(String.valueOf(para.get("pageNum")));
        Integer pageSize = Integer.parseInt(String.valueOf(para.get("pageSize")));
        HashMap<String, Object> hs = new HashMap<>();
        //设置分页信息
        PageHelper.startPage(pageNum, pageSize);
        List<ConstantItem> constantItemList = constantItemService.hightSerchCos(constantItem);
        PageInfo<ConstantItem> info = new PageInfo<>(constantItemList);
        //给map添加数据
        hs.put("totalpage", info.getTotal());
        hs.put("pagenum", info.getPageNum());
        hs.put("conitem", constantItemList);
        if (constantItemList.size() > 0) {//如果获取到数据
            hs.put("meta", new Constance("获取到"+info.getTotal()+"条数据", 200));
        } else {
            hs.put("meta", new Constance("没有查询到数据", 401));
        }
        return objectMapper.writeValueAsString(hs);
    }

}
